package com.stu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stu.commons.ServerResponse;
import com.stu.pojo.Cart;
import com.stu.pojo.CustomerTokenDTO;
import com.stu.pojo.Product;
import com.stu.pojo.Users;
import com.stu.service.ICartService;
import com.stu.service.IProductService;
import com.stu.service.IUsersService;
import com.stu.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IUsersService usersService;

    // 购物车页面
    @GetMapping("shopcart")
    @ResponseBody
    public ServerResponse loadcartData(HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        String userIdStr = JwtUtil.getUserIdFromToken(token);
        Integer userId = Integer.parseInt(userIdStr);

        Integer cartNum = 0;
        Users user = usersService.selectById(userId);

        if (token != null) {
            cartNum = cartService.getUserCartProductCount(user.getUserId());
        }

        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUserId());

        List<Cart> cartList = cartService.list(wrapper);
        List<Product> productList = productService.getListByCart(cartList);

        List<String> productDetails = new ArrayList<>();
        for (Product product : productList) {
            String detail = product.getProductName() + product.getProductType()
                    + product.getProductColor() + product.getProductSize();
            productDetails.add(detail);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("users", user);
        map.put("cartNum", cartNum);
        map.put("productList", productList);
        map.put("prodetails", productDetails);
        map.put("cartList", cartList);

        System.out.println(map);
        System.out.println("CartController返回1return");

        return ServerResponse.success("ok", map);
    }

    // 加入购物车
    @PostMapping("addToCart")
    public ServerResponse AddProductToCart(@RequestBody Cart cart, HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        String userIdStr = JwtUtil.getUserIdFromToken(token);
        Integer userId = Integer.parseInt(userIdStr);

        Integer proId = cart.getProductId();
        Integer proNum = cart.getProductNum();

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localdatetime============" + localDateTime);

        Cart cart1 = new Cart();
        cart1.setUserId(userId);
        cart1.setProductNum(proNum);
        cart1.setProductId(proId);
        cart1.setPurchaseTime(localDateTime);
        cart1.setCartStatus(1);

        System.out.println(cart1);
        ServerResponse result = cartService.savecart(cart1);
        System.out.println("CartController返回2" + result);
        return result;
    }
}

